//
//  YZWorkNoteManagePickerView.m
//  VET
//
//  Created by 刘文靖 on 2018/7/11.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZWorkNoteManagePickerView.h"
#import <YYKit/UIControl+YYAdd.h>
#define kPickerViewHeight 258

@interface YZWorkNoteManagePickerView ()
@property (nonatomic, strong) UIView *pickerView;

@end

@implementation YZWorkNoteManagePickerView
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initUI];
    }
    return self;
}

- (void)initUI {
    self.frame = kScreenBounds;
    // 设置子视图的宽度随着父视图变化
    self.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    // 背景遮罩图层
    [self addSubview:self.backgroundView];
    // 弹出视图
    [self addSubview:self.alertView];
    self.alertView.frame = CGRectMake(0, kScreenHeight - kPickerViewHeight, kScreenWidth, kPickerViewHeight);
    self.alertView.backgroundColor = kColorWithHex(0xF6F7F7);
    // 设置弹出视图子视图
    UIView *topInfoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.alertView.frame.size.width, kPickerViewHeight - 66)];
    //    topInfoView.layer.cornerRadius = 13;
    //    topInfoView.layer.masksToBounds = YES;
    [self.alertView addSubview:topInfoView];
    // 添加字符串选择器
    [topInfoView addSubview:self.pickerView];
    
    UIButton *cancelButton = [[UIButton alloc] init];
    cancelButton.backgroundColor = [UIColor whiteColor];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton setTitleColor:kColorWithHexAlpha(0x393939, 1.0) forState:UIControlStateNormal];
    cancelButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
    cancelButton.frame = CGRectMake(0, self.alertView.frame.size.height - 54, self.alertView.frame.size.width, 54);
    //    cancelButton.layer.cornerRadius = 13;
    [cancelButton addTarget:self action:@selector(clickLeftBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.alertView addSubview:cancelButton];
}


#pragma mark - 字符串选择器
- (UIView *)pickerView {
    if (!_pickerView) {
        _pickerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kPickerViewHeight - 67)];
        _pickerView.backgroundColor = [UIColor whiteColor];
        // 设置子视图的大小随着父视图变化
        _pickerView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
        CGFloat btnHeight = 64;
        CGFloat topMargin = 14;
        UIButton *archiveBtn = ({
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, btnHeight)];
            btn.backgroundColor = [UIColor whiteColor];
            UILabel *topLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, topMargin, kScreenWidth, 16)];
            topLabel.textAlignment = NSTextAlignmentCenter;
            topLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
            topLabel.textColor = kColorWithHex(0x434343);
            topLabel.text = @"编辑归档";
            [btn addSubview:topLabel];
            
            UILabel *bottomLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, topLabel.bottom + 10, kScreenWidth, 12)];
            bottomLabel.textAlignment = NSTextAlignmentCenter;
            bottomLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
            bottomLabel.text = @"将当前笔记内容存储在猪只或猪群下";
            bottomLabel.textColor = kColorWithHex(0x9b9b9b);
            [btn addSubview:bottomLabel];
            
            UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, btnHeight -1, kScreenWidth, 1)];
            lineView.backgroundColor = kColorWithHexAlpha(0xD8D8D8,0.3);
            lineView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
            [btn addSubview:lineView];
            btn;
        });
        [_pickerView addSubview:archiveBtn];
        
        UIButton *trackBtn = ({
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, btnHeight, kScreenWidth, btnHeight)];
            btn.backgroundColor = [UIColor whiteColor];
            UILabel *topLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, topMargin, kScreenWidth, 16)];
            topLabel.textAlignment = NSTextAlignmentCenter;
            topLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
            topLabel.textColor = kColorWithHex(0x434343);
            topLabel.text = @"待办跟踪";
            [btn addSubview:topLabel];
            
            UILabel *bottomLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, topLabel.bottom + 10, kScreenWidth, 12)];
            bottomLabel.textAlignment = NSTextAlignmentCenter;
            bottomLabel.text = @"新增一个待办任务用于提醒跟踪当前笔记后续工作";
            bottomLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
            bottomLabel.textColor = kColorWithHex(0x9b9b9b);
            [btn addSubview:bottomLabel];
            
            UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, btnHeight -1, kScreenWidth, 1)];
            lineView.backgroundColor = kColorWithHexAlpha(0xD8D8D8,0.3);
            lineView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
            [btn addSubview:lineView];
            
            btn;
        });
        [_pickerView addSubview:trackBtn];
        
        UIButton *deleteBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, btnHeight * 2, kScreenWidth, btnHeight)];
        [deleteBtn setTitle:@"删除笔记" forState:UIControlStateNormal];
        [deleteBtn setTitleColor:kColorWithHex(0x393939) forState:UIControlStateNormal];
        deleteBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
        deleteBtn.backgroundColor = [UIColor whiteColor];
        [_pickerView addSubview:deleteBtn];
        @weakify(self)
        [archiveBtn addBlockForControlEvents:UIControlEventTouchUpInside block:^(id sender) {
            @strongify(self)
            [self dismissWithAnimation:NO];
            if (self.resultBlock) {
                self.resultBlock(0);
            }
        }];
        
        [trackBtn addBlockForControlEvents:UIControlEventTouchUpInside block:^(id sender) {
            @strongify(self)
            [self dismissWithAnimation:NO];
            if (self.resultBlock) {
                self.resultBlock(1);
            }
        }];
        
        [deleteBtn addBlockForControlEvents:UIControlEventTouchUpInside block:^(id sender) {
            @strongify(self)
            [self dismissWithAnimation:NO];
            if (self.resultBlock) {
                self.resultBlock(2);
            }
        }];
        
    }
    return _pickerView;
}

#pragma mark - 背景视图的点击事件
- (void)didTapBackgroundView:(UITapGestureRecognizer *)sender {
    [self dismissWithAnimation:NO];
    if (self.cancelBlock) {
        self.cancelBlock();
    }
}

#pragma mark - 取消按钮的点击事件
- (void)clickLeftBtn {
    [self dismissWithAnimation:YES];
    if (self.cancelBlock) {
        self.cancelBlock();
    }
}

#pragma mark - 弹出视图方法
- (void)showWithAnimation:(BOOL)animation {
    //1. 获取当前应用的主窗口
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    [keyWindow addSubview:self];
    if (animation) {
        // 动画前初始位置
        CGRect rect = self.alertView.frame;
        rect.origin.y = kScreenHeight;
        self.alertView.frame = rect;
        // 浮现动画
        [UIView animateWithDuration:0.3 animations:^{
            CGRect rect = self.alertView.frame;
            rect.origin.y -= kPickerViewHeight;
            self.alertView.frame = rect;
        }];
    }
}

#pragma mark - 关闭视图方法
- (void)dismissWithAnimation:(BOOL)animation {
    // 关闭动画
    [UIView animateWithDuration:0.2 animations:^{
        CGRect rect = self.alertView.frame;
        rect.origin.y += kPickerViewHeight;
        self.alertView.frame = rect;
        self.backgroundView.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
@end
